#include <iostream>
#include "Thread.hpp"
#include "RingBuffer.hpp"
#include <unistd.h>
#include <vector>
#include <time.h>
#include <sys/types.h>
using namespace ThreadMoudle;



using func_t = std::function<void()>;
using RB = RingBufferModule::RingBuffer<int>;
const int N = 20;


int gap = 20;
const int gcap = 5;
const int g_c_num = 10;
const int g_p_num = 1;

int data = 10;

ThreadMoudle::Mutex gmtx;

void Log()
{
    int cnt = 5;
    while (cnt--)
    {
        char time_str[50];
        time_t now = time(nullptr);
        tm *local_time = localtime(&now);

        // 格式化日期时间
        sprintf(time_str, "[LOG:当前时间: %04d-%02d-%02d %02d:%02d:%02d]\n",
                local_time->tm_year + 1900, // 年份需要加1900
                local_time->tm_mon + 1,     // 月份从0开始，需要加1
                local_time->tm_mday,
                local_time->tm_hour,
                local_time->tm_min,
                local_time->tm_sec);
        std::cout << time_str << std::endl;
    }
}

void Message()
{
    int cnt = 5;
    while (cnt--)
    {
        char str[50];

        // 格式化日期时间
        sprintf(str, "Ciallo～(∠・ω ∠)⌒☆\n");

        for (int i = 1; i <= (rand() % 70); ++i)
            std::cout << ' ';

        std::cout << str << std::endl;
    }
}

void Consumer(RB *rb)
{
    sleep(6);
    int data;
    while (true)
    {
        // 1. 从bq拿到数据
        rb->Pop(data);

        // 2.做处理
        printf("Consumer, 消费了一个数据:%d\n",data);
        // data();
    }
}

void Producer(RB *rb)
{

    while (true)
    {
        sleep(1);
        {
            ThreadMoudle::LockGuard lockguard(gmtx);
            // 1. 从外部获取数据
            // 2. 生产到bq中
            rb->Enqueue(data);

            printf("producter 生产了一个数据:%d\n",data);

            data++;
        }
    }
}

int main()
{
    std::vector<Thread<RB *>> threads;
    RB *rb = new RB(gcap);

    for (int i = 1; i <= g_c_num; ++i)
        threads.emplace_back(Consumer, rb);

    for (int i = 1; i <= g_p_num; ++i)
        threads.emplace_back(Producer, rb);

    for (auto &t : threads)
        t.Start();
    for (auto &t : threads)
    {
        int n = t.Join();
        std::cout << "join " << t.Name() << " = " << n << std::endl;
    }

    return 0;
}
